package com.ruoyi.pcwl.app;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

public class WeChatAPIExample {



    public static void main(String[] args) throws JsonProcessingException {
        // 替换成你的公众号信息
        String appId = "wx78bd823c74e1b7cc";
        String appSecret = "93129c5094bc2107b6189a4cd7f89300";
        String accessToken = getAccessToken(appId, appSecret);

        ObjectMapper objectMapper=new ObjectMapper();

        JsonNode rootNode = objectMapper.readTree(accessToken);
        Map<String, Object> map = objectMapper.convertValue(rootNode, new TypeReference<Map<String, Object>>() {});

        System.out.println("Converted Map:");
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
        // 替换成你的公众号的用户 ID
        String openid = "用户的openid";

        // 获取文章列表
        String articleList = getArticleList((String) map.get("access_token"), openid);
        System.out.println(articleList);
    }

    // 获取 access_token
    private static String getAccessToken(String appId, String appSecret) {
        String url = "https://api.weixin.qq.com/cgi-bin/token" +
                "?grant_type=client_credential" +
                "&appid=" + appId +
                "&secret=" + appSecret;

        try {
            HttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            // 解析 JSON 获取 access_token
            // 请根据实际返回的 JSON 数据结构进行解析
            // 这里仅作为示例，实际使用中需要根据返回的 JSON 结构提取 access_token 的值
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取文章列表
    private static String getArticleList(String accessToken, String openid) {
        String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material" +
                "?access_token=" + accessToken;

        String requestBody = "{\"type\":\"news\",\"offset\":0,\"count\":10}";

        try {
            HttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setEntity(new StringEntity(requestBody));

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            // 解析 JSON 获取文章列表
            // 请根据实际返回的 JSON 数据结构进行解析
            // 这里仅作为示例，实际使用中需要根据返回的 JSON 结构提取文章列表的值
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
